/*
 * Created on 15.01.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.DBWrapper;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.EditAlerterForm;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.usr.messaging.Alert;
import ro.gateway.aida.usr.messaging.Alerter;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.Utils;


/**
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 */
public class AlerterAction extends MultiTilesAction {
    private DBWrapper db;

    public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        if (user == null)
            return mapping.findForward("plslogin");

        db = DBWrapper.getManager(token);

        return modeSelect(mapping, form, request, response);
    }

    public ActionForward modeView(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        if (request.getParameter("refresh") != null) {
            long id = Long.parseLong(request.getParameter("id"));
            Alerter m = (Alerter) db.get(new Alerter(id, -1));
            HttpSession session = request.getSession();

            if (m == null)
                return mapping.findForward("notfound");

            session.setAttribute("CurrentAlerter", m);
        }
		Utils.setBreadCrumbs(request,"bc.alerter.view");	
        return mapping.findForward("view");
    }

    public ActionForward modeWidget(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        HttpSession session = request.getSession();

        if (session.getAttribute("CurrentAlerter") == null)
            return mapping.findForward("notfound");

        return mapping.findForward("widget");
    }

    public ActionForward modeList(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
        List sl = (List) session.getAttribute(LIST);

        if (sl == null)
            return modeRefresh(mapping, form, request, response);
        else {
        	
        	PageToken.tokenizeList( request,  sc, sl, "alerters");
            Utils.setBreadCrumbs(request,"bc.alerter.list");	
            return mapping.findForward("list");
        }
    }

    public ActionForward modeRefresh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        HttpSession session = request.getSession();

        session.setAttribute(LIST, db.get(new Alerter(-1, user.getId())));

        List sl = (List) session.getAttribute(LIST);
        PageToken.tokenizeList( request,  sc, sl, "alerters");
        Utils.setBreadCrumbs(request,"bc.alerter.list");	
        return mapping.findForward("list");
    }

    public ActionForward modeEdit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        long id = Long.parseLong(request.getParameter("id"));
        EditAlerterForm eaf = (EditAlerterForm) form;

        Alerter m = (Alerter) db.get(new Alerter(id, -1));
        eaf.setId(m.getId());
        eaf.setEnabled(m.isEnabled());
        eaf.setMailable(m.isMailable());
        eaf.setName(m.getName());
        eaf.setPurpose(m.getPurpose());
        eaf.setPriority(m.getPriority());
        eaf.setIntersection(m.isIntersection());
		Utils.setBreadCrumbs(request,"bc.alerter.edit");	
        return mapping.getInputForward();
    }

    public ActionForward modeSave(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        EditAlerterForm eaf = (EditAlerterForm) form;
        Alerter m = new Alerter(eaf.getId(), user.getId());
        m.setEnabled(eaf.isEnabled());
        m.setAlerterType(Alert.FILTER_ALERT);
        m.setName(eaf.getName());
        m.setMailable(eaf.isMailable());
        m.setPriority(eaf.getPriority());
        m.setPurpose(eaf.getPurpose());
        m.setIntersection(eaf.isIntersection());
        db.synchronize(m);

        return modeRefresh(mapping, form, request, response);
    }

	public ActionForward modeDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			User user = UserUtils.getLogged(request, response);

			String[] delalerters = request.getParameterValues("delalerters");

			if (delalerters != null)
				for(int i = 0; i < delalerters.length; i++)
				
					db.delete(new Alerter(Long.parseLong(delalerters[i]),-1));
				
					
			return modeRefresh(mapping, form, request, response);
		}


    public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String mode = request.getParameter("mode");

        if ("view".equals(mode))
            return modeView(mapping, form, request, response);

        if ("refresh".equals(mode))
            return modeRefresh(mapping, form, request, response);

        if ("list".equals(mode))
            return modeList(mapping, form, request, response);

        if ("edit".equals(mode))
            return modeEdit(mapping, form, request, response);
		
		if ("widget".equals(mode))
					 return modeWidget(mapping, form, request, response);

        if ("save".equals(mode))
            return modeSave(mapping, form, request, response);
            
		if ("delete".equals(mode))
					  return modeDelete(mapping, form, request, response);


        return mapping.getInputForward();
    }

    public static final String LIST = "Alerter_LIST";
}
